home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpdfe.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  12.1 KB  |  394 lines

  1. /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpdfe.c,v 1.4.2.1 2000/11/09 20:37:29 rayjj Exp $ */
  20. /* Embedded font writing for pdfwrite driver. */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsmatrix.h"
  26. #include "gxfcid.h"
  27. #include "gxfont.h"
  28. #include "gxfont0.h"
  29. #include "gdevpdfx.h"
  30. #include "gdevpdff.h"
  31. #include "gdevpsf.h"
  32. #include "scommon.h"
  33.  
  34. /* ---------------- Utilities ---------------- */
  35.  
  36. /* Begin writing FontFile* data. */
  37. private int
  38. pdf_begin_fontfile(gx_device_pdf *pdev, long FontFile_id,
  39.            long *plength_id, const char *entries,
  40.            long len, long *pstart, psdf_binary_writer *pbw)
  41. {
  42.     stream *s;
  43.  
  44.     pdf_open_separate(pdev, FontFile_id);
  45.     *plength_id = pdf_obj_ref(pdev);
  46.     s = pdev->strm;
  47.     pprintld1(s, "<</Length %ld 0 R", *plength_id);
  48.     if (!pdev->binary_ok)
  49.     pputs(s, "/Filter/ASCII85Decode");
  50.     if (entries)
  51.     pputs(pdev->strm, entries);
  52.     pprintld1(pdev->strm, "/Length1 %ld>>stream\n", len);
  53.     *pstart = pdf_stell(pdev);
  54.     return psdf_begin_binary((gx_device_psdf *)pdev, pbw);
  55. }
  56.  
  57. /* Finish writing FontFile* data. */
  58. private int
  59. pdf_end_fontfile(gx_device_pdf *pdev, long start, long length_id,
  60.          psdf_binary_writer *pbw)
  61. {
  62.     stream *s = pdev->strm;
  63.     long length;
  64.  
  65.     psdf_end_binary(pbw);
  66.     pputs(s, "\n");
  67.     length = pdf_stell(pdev) - start;
  68.     pputs(s, "endstream\n");
  69.     pdf_end_separate(pdev);
  70.     pdf_open_separate(pdev, length_id);
  71.     pprintld1(pdev->strm, "%ld\n", length);
  72.     pdf_end_separate(pdev);
  73.     return 0;
  74. }
  75.  
  76. /* ---------------- Individual font types ---------------- */
  77.  
  78. /* ------ Type 1 family ------ */
  79.  
  80. /*
  81.  * Acrobat Reader apparently doesn't accept CFF fonts with Type 1
  82.  * CharStrings, so we need to convert them.
  83.  */
  84. #define TYPE2_OPTIONS WRITE_TYPE2_CHARSTRINGS
  85.  
  86. /* Write the FontFile[3] data for an embedded Type 1, Type 2, or */
  87. /* CIDFontType 0 font. */
  88. private int
  89. pdf_embed_font_as_type1(gx_device_pdf *pdev, gs_font_type1 *font,
  90.             long FontFile_id, gs_glyph subset_glyphs[256],
  91.             uint subset_size, const gs_const_string *pfname)
  92. {
  93.     stream poss;
  94.     int lengths[3];
  95.     int code;
  96.     long length_id;
  97.     long start;
  98.     psdf_binary_writer writer;
  99. #define MAX_INT_CHARS ((sizeof(int) * 8 + 2) / 3)
  100.     char lengths_str[9 + MAX_INT_CHARS + 11];  /* /Length2 %d/Length3 0\0 */
  101. #undef MAX_INT_CHARS
  102.  
  103.     swrite_position_only(&poss);
  104.     /*
  105.      * We omit the 512 zeros and the cleartomark, and set Length3 to 0.
  106.      * Note that the interpreter adds them implicitly (per documentation),
  107.      * so we must set MARK so that the encrypted portion pushes a mark on
  108.      * the stack.
  109.      *
  110.      * NOTE: the options were set to 0 in the first checked-in version of
  111.      * this file.  We can't explain this: Acrobat Reader requires eexec
  112.      * encryption, so the code can't possibly have worked.
  113.      *
  114.      * Acrobat Reader 3 allows lenIV = -1 in Type 1 fonts, but Acrobat
  115.      * Reader 4 doesn't.  Therefore, we can't allow it.
  116.      */
  117. #define TYPE1_OPTIONS (WRITE_TYPE1_EEXEC | WRITE_TYPE1_EEXEC_MARK |\
  118.                WRITE_TYPE1_WITH_LENIV)
  119.     code = psf_write_type1_font(&poss, font, TYPE1_OPTIONS,
  120.                  subset_glyphs, subset_size, pfname, lengths);
  121.     if (code < 0)
  122.     return code;
  123.     sprintf(lengths_str, "/Length2 %d/Length3 0", lengths[1]);
  124.     code = pdf_begin_fontfile(pdev, FontFile_id, &length_id,
  125.                   lengths_str, lengths[0], &start, &writer);
  126.     if (code < 0)
  127.     return code;
  128.     psf_write_type1_font(writer.strm, font, TYPE1_OPTIONS, subset_glyphs,
  129.              subset_size, pfname, lengths /*ignored*/);
  130. #undef TYPE1_OPTIONS
  131.     pdf_end_fontfile(pdev, start, length_id, &writer);
  132.     return 0;
  133. }
  134.  
  135. /* Embed a font as Type 2. */
  136. private int
  137. pdf_embed_font_as_type2(gx_device_pdf *pdev, gs_font_type1 *font,
  138.             long FontFile_id, gs_glyph subset_glyphs[256],
  139.             uint subset_size, const gs_const_string *pfname)
  140. {
  141.     stream poss;
  142.     int code;
  143.     long length_id;
  144.     long start;
  145.     psdf_binary_writer writer;
  146.     int options = TYPE2_OPTIONS |
  147.     (pdev->CompatibilityLevel < 1.3 ? WRITE_TYPE2_AR3 : 0);
  148.  
  149.     swrite_position_only(&poss);
  150.     code = psf_write_type2_font(&poss, font, options,
  151.                 subset_glyphs, subset_size, pfname);
  152.     if (code < 0)
  153.     return code;
  154.     code = pdf_begin_fontfile(pdev, FontFile_id, &length_id,
  155.                   "/Subtype/Type1C", stell(&poss),
  156.                   &start, &writer);
  157.     if (code < 0)
  158.     return code;
  159.     code = psf_write_type2_font(writer.strm, font, options,
  160.                 subset_glyphs, subset_size, pfname);
  161.     pdf_end_fontfile(pdev, start, length_id, &writer);
  162.     return 0;
  163. }
  164.  
  165. /* Embed a Type 1 or Type 2 font. */
  166. private int
  167. pdf_embed_font_type1(gx_device_pdf *pdev, gs_font_type1 *font,
  168.              long FontFile_id, gs_glyph subset_glyphs[256],
  169.              uint subset_size, const gs_const_string *pfname)
  170. {
  171.     switch (((const gs_font *)font)->FontType) {
  172.     case ft_encrypted:
  173.     if (pdev->CompatibilityLevel < 1.2)
  174.         return pdf_embed_font_as_type1(pdev, font, FontFile_id,
  175.                        subset_glyphs, subset_size, pfname);
  176.     /* For PDF 1.2 and later, write Type 1 fonts as Type1C. */
  177.     case ft_encrypted2:
  178.     return pdf_embed_font_as_type2(pdev, font, FontFile_id,
  179.                        subset_glyphs, subset_size, pfname);
  180.     default:
  181.     return_error(gs_error_rangecheck);
  182.     }
  183. }
  184.  
  185. /* Embed a CIDFontType0 font. */
  186. private int
  187. pdf_embed_font_cid0(gx_device_pdf *pdev, gs_font_cid0 *font,
  188.             long FontFile_id, const byte *subset_cids,
  189.             uint subset_size, const gs_const_string *pfname)
  190. {
  191.     stream poss;
  192.     int code;
  193.     long length_id;
  194.     long start;
  195.     psdf_binary_writer writer;
  196.  
  197.     if (pdev->CompatibilityLevel < 1.2)
  198.     return_error(gs_error_rangecheck);
  199.     /*
  200.      * It would be wonderful if we could avoid duplicating nearly all of
  201.      * pdf_embed_font_as_type2, but we don't see how to do it.
  202.      */
  203.     swrite_position_only(&poss);
  204.     code = psf_write_cid0_font(&poss, font, TYPE2_OPTIONS,
  205.                    subset_cids, subset_size, pfname);
  206.     if (code < 0)
  207.     return code;
  208.     code = pdf_begin_fontfile(pdev, FontFile_id, &length_id,
  209.                   "/Subtype/CIDFontType0C", stell(&poss),
  210.                   &start, &writer);
  211.     if (code < 0)
  212.     return code;
  213.     code = psf_write_cid0_font(writer.strm, font, TYPE2_OPTIONS,
  214.                    subset_cids, subset_size, pfname);
  215.     pdf_end_fontfile(pdev, start, length_id, &writer);
  216.     return 0;
  217. }
  218.  
  219. /* ------ TrueType family ------ */
  220.  
  221. /* Embed a TrueType font. */
  222. private int
  223. pdf_embed_font_type42(gx_device_pdf *pdev, gs_font_type42 *font,
  224.               long FontFile_id, gs_glyph subset_glyphs[256],
  225.               uint subset_size, const gs_const_string *pfname)
  226. {
  227.     /* Acrobat Reader 3 doesn't handle cmap format 6 correctly. */
  228.     const int options = WRITE_TRUETYPE_CMAP | WRITE_TRUETYPE_NAME |
  229.     (pdev->CompatibilityLevel <= 1.2 ?
  230.      WRITE_TRUETYPE_NO_TRIMMED_TABLE : 0);
  231.     stream poss;
  232.     int code;
  233.     long length_id;
  234.     long start;
  235.     psdf_binary_writer writer;
  236.  
  237.     swrite_position_only(&poss);
  238.     code = psf_write_truetype_font(&poss, font, options,
  239.                    subset_glyphs, subset_size, pfname);
  240.     if (code < 0)
  241.     return code;
  242.     code = pdf_begin_fontfile(pdev, FontFile_id, &length_id, NULL,
  243.                   stell(&poss), &start, &writer);
  244.     if (code < 0)
  245.     return code;
  246.     psf_write_truetype_font(writer.strm, font, options,
  247.                 subset_glyphs, subset_size, pfname);
  248.     pdf_end_fontfile(pdev, start, length_id, &writer);
  249.     return 0;
  250. }
  251.  
  252. /* Embed a CIDFontType2 font. */
  253. private int
  254. pdf_embed_font_cid2(gx_device_pdf *pdev, gs_font_cid2 *font,
  255.             long FontFile_id, const byte *subset_bits,
  256.             uint subset_size, const gs_const_string *pfname)
  257. {
  258.     /* CIDFontType 2 fonts don't use cmap, name, OS/2, or post. */
  259. #define OPTIONS 0
  260.     stream poss;
  261.     int code;
  262.     long length_id;
  263.     long start;
  264.     psdf_binary_writer writer;
  265.  
  266.     swrite_position_only(&poss);
  267.     code = psf_write_cid2_font(&poss, font, OPTIONS,
  268.                    subset_bits, subset_size, pfname);
  269.     if (code < 0)
  270.     return code;
  271.     code = pdf_begin_fontfile(pdev, FontFile_id, &length_id, NULL,
  272.                   stell(&poss), &start, &writer);
  273.     if (code < 0)
  274.     return code;
  275.     psf_write_cid2_font(writer.strm, font, OPTIONS,
  276.             subset_bits, subset_size, pfname);
  277. #undef OPTIONS
  278.     pdf_end_fontfile(pdev, start, length_id, &writer);
  279.     return 0;
  280. }
  281.  
  282. /* ---------------- Entry point ---------------- */
  283.  
  284. /*
  285.  * Write the FontDescriptor and FontFile* data for an embedded font.
  286.  * Return a rangecheck error if the font can't be embedded.
  287.  */
  288. int
  289. pdf_write_embedded_font(gx_device_pdf *pdev, pdf_font_descriptor_t *pfd)
  290. {
  291.     gs_font *font = pfd->base_font;
  292.     gs_const_string font_name;
  293.     byte *fnchars = pfd->FontName.chars;
  294.     uint fnsize = pfd->FontName.size;
  295.     bool do_subset = pfd->subset_ok && pdev->params.SubsetFonts &&
  296.     pdev->params.MaxSubsetPct > 0;
  297.     long FontFile_id = pfd->FontFile_id;
  298.     gs_glyph subset_glyphs[256];
  299.     gs_glyph *subset_list = 0;    /* for non-CID fonts */
  300.     byte *subset_bits = 0;    /* for CID fonts */
  301.     uint subset_size = 0;
  302.     gs_matrix save_mat;
  303.     int code;
  304.  
  305.     /* Determine whether to subset the font. */
  306.     if (do_subset) {
  307.     int used, i, total, index;
  308.     gs_glyph ignore_glyph;
  309.  
  310.     for (i = 0, used = 0; i < pfd->chars_used.size; ++i)
  311.         used += byte_count_bits[pfd->chars_used.data[i]];
  312.     for (index = 0, total = 0;
  313.          (font->procs.enumerate_glyph(font, &index, GLYPH_SPACE_INDEX,
  314.                       &ignore_glyph), index != 0);
  315.          )
  316.         ++total;
  317.     if ((double)used / total > pdev->params.MaxSubsetPct / 100.0)
  318.         do_subset = false;
  319.     }
  320.  
  321.     /* Generate an appropriate font name. */
  322.     if (pdf_has_subset_prefix(fnchars, fnsize)) {
  323.     /* Strip off any existing subset prefix. */
  324.     fnsize -= SUBSET_PREFIX_SIZE;
  325.     memmove(fnchars, fnchars + SUBSET_PREFIX_SIZE, fnsize);
  326.     }
  327.     if (do_subset) {
  328.     memmove(fnchars + SUBSET_PREFIX_SIZE, fnchars, fnsize);
  329.     pdf_make_subset_prefix(fnchars, FontFile_id);
  330.     fnsize += SUBSET_PREFIX_SIZE;
  331.     }
  332.     font_name.data = fnchars;
  333.     font_name.size = pfd->FontName.size = fnsize;
  334.     code = pdf_write_FontDescriptor(pdev, pfd);
  335.     if (code >= 0) {
  336.     pfd->written = true;
  337.  
  338.     /*
  339.      * Finally, write the font (or subset), using the original
  340.      * (unscaled) FontMatrix.
  341.      */
  342.     save_mat = font->FontMatrix;
  343.     font->FontMatrix = pfd->orig_matrix;
  344.     switch (font->FontType) {
  345.     case ft_composite:
  346.         /* Nothing to embed -- the descendant fonts do it all. */
  347.         break;
  348.     case ft_encrypted:
  349.     case ft_encrypted2:
  350.         if (do_subset) {
  351.         subset_size = psf_subset_glyphs(subset_glyphs, font,
  352.                         pfd->chars_used.data);
  353.         subset_list = subset_glyphs;
  354.         }
  355.         code = pdf_embed_font_type1(pdev, (gs_font_type1 *)font,
  356.                     FontFile_id, subset_list,
  357.                     subset_size, &font_name);
  358.         break;
  359.     case ft_TrueType:
  360.         if (do_subset) {
  361.         subset_size = psf_subset_glyphs(subset_glyphs, font,
  362.                         pfd->chars_used.data);
  363.         subset_list = subset_glyphs;
  364.         }
  365.         code = pdf_embed_font_type42(pdev, (gs_font_type42 *)font,
  366.                      FontFile_id, subset_list,
  367.                      subset_size, &font_name);
  368.         break;
  369.     case ft_CID_encrypted:
  370.         if (do_subset) {
  371.         subset_size = pfd->chars_used.size << 3;
  372.         subset_bits = pfd->chars_used.data;
  373.         }
  374.         code = pdf_embed_font_cid0(pdev, (gs_font_cid0 *)font,
  375.                        FontFile_id, subset_bits,
  376.                        subset_size, &font_name);
  377.         break;
  378.     case ft_CID_TrueType:
  379.         if (do_subset) {
  380.         subset_size = pfd->chars_used.size << 3;
  381.         subset_bits = pfd->chars_used.data;
  382.         }
  383.         code = pdf_embed_font_cid2(pdev, (gs_font_cid2 *)font,
  384.                        FontFile_id, subset_bits,
  385.                        subset_size, &font_name);
  386.         break;
  387.     default:
  388.         code = gs_note_error(gs_error_rangecheck);
  389.     }
  390.     font->FontMatrix = save_mat;
  391.     }
  392.     return code;
  393. }
  394.